﻿Public Class FrmKhachHang

   
    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()

    End Sub
    Sub Them()
        Dim kh As KhachHangDTO = New KhachHangDTO()
        kh.TenKhachHang = txtTenKhachHang.Text
        kh.DiaChi = txtDiaChi.Text
        kh.Email = txtEmail.Text
        kh.DienThoai = txtDienThoai.Text

        KhachHangDAO.Insert(kh)
        LoadLenLuoi()
    End Sub
    Sub LoadLenLuoi()
        Dim ds As DataTable
        ds = KhachHangDAO.SelectAll()
        dgvDSKhachHang.DataSource = ds

    End Sub
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Them()
    End Sub
    Sub Sua()
        Dim kh As KhachHangDTO = New KhachHangDTO()
        kh.MaKhachHang = txtMaKhachHang.Text
        kh.TenKhachHang = txtTenKhachHang.Text
        kh.DiaChi = txtDiaChi.Text
        kh.Email = txtEmail.Text
        kh.DienThoai = txtDienThoai.Text

        KhachHangDAO.Update(kh)
        LoadLenLuoi()
    End Sub
    Sub HienThiChinhSua()
        If (dgvDSKhachHang.SelectedRows.Count > 0) Then
            txtMaKhachHang.Text = dgvDSKhachHang.SelectedRows(0).Cells(0).Value.ToString()
            txtTenKhachHang.Text = dgvDSKhachHang.SelectedRows(0).Cells(1).Value.ToString()
            txtDiaChi.Text = dgvDSKhachHang.SelectedRows(0).Cells(2).Value.ToString()
            txtEmail.Text = dgvDSKhachHang.SelectedRows(0).Cells(3).Value.ToString()
            txtDienThoai.Text = dgvDSKhachHang.SelectedRows(0).Cells(4).Value.ToString()
        End If
    End Sub
    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Sua()
    End Sub

    Private Sub FrmKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadLenLuoi()
    End Sub

    Private Sub dgvDSKhachHang_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDSKhachHang.CellContentClick
        LoadLenLuoi()
    End Sub

    Private Sub dgvDSKhachHang_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDSKhachHang.SelectionChanged
        HienThiChinhSua()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtMaKhachHang.Text = ""
        txtTenKhachHang.Text = ""
        txtDiaChi.Text = ""
        txtEmail.Text = ""
        txtDienThoai.Text = ""
    End Sub
    Sub Xoa()
        Dim kh As KhachHangDTO = New KhachHangDTO()
        kh.MaKhachHang = Convert.ToInt32(txtMaKhachHang.Text)
        KhachHangDAO.Delete(kh)
        LoadLenLuoi()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Xoa()
    End Sub
End Class